<?php

namespace App\Recharge\Api;

use App\Models\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class Sichuanjianuo extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $array = [
            'Service'   => 'SubmitOrder',
            'UserId'    => $bean->getSupplierNum(),
            'BizType'   => 'ECARD',
            'OrderNo'   => $bean->getOrderNo(),//订单号
            'ProductId' => $bean->getGoodsId(),
            'AccountVal'=> $bean->getAccount(),
            'BuyNum'    => $bean->getRechargeNum(),
            'Time'      => time(),
        ];
        ksort($array);
        $array['Sign'] = md5('AccountVal'.$array['AccountVal'].'BizType'.$array['BizType'].
            'BuyNum'.$array['BuyNum'].'OrderNo'.$array['OrderNo'].'ProductId'.$array['ProductId'].
            'Service'.$array['Service'].'Time'.$array['Time'].'UserId'.$array['UserId'].
            $bean->getSecretKey());
        return $array;

    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('OrderStatus', '');
        $order_sn = $request->input('OrderNo', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['SUCCESS', 'FAILED']))->setRechargeSuccess($status == 'SUCCESS');
    }

    /**
     * @return JsonResponse
     */
    public function apiNotifySuccess(): JsonResponse
    {
        return response()->json(['code' => 0]);
    }
}
